Easy2Siksha
3. Move to the Next Pair:
o Move to the next pair of items (second and third).
o Repeat the comparison and swapping if necessary.
4. Connue to the End:
o Keep comparing and swapping unl you reach the end of the list.
5. Repeat the Process:
o Aer reaching the end, go back to the beginning and repeat the process.
o Each pass through the list places the next largest item in its correct posion.
6. Stop When Sorted:
o The sorng process stops when a complete pass through the list results in no
swaps. This means the list is fully sorted.
Visualizing Bubble Sort with an Example
Let's sort the list [5, 3, 8, 4, 2] using Bubble Sort:
• First Pass:
1. Compare 5 and 3. Since 5 > 3, swap them. List: [3, 5, 8, 4, 2].
2. Compare 5 and 8. No swap needed. List remains: [3, 5, 8, 4, 2].
3. Compare 8 and 4. Since 8 > 4, swap them. List: [3, 5, 4, 8, 2].
4. Compare 8 and 2. Since 8 > 2, swap them. List: [3, 5, 4, 2, 8].
• Second Pass:
1. Compare 3 and 5. No swap needed. List remains: [3, 5, 4, 2, 8].
2. Compare 5 and 4. Since 5 > 4, swap them. List: [3, 4, 5, 2, 8].
3. Compare 5 and 2. Since 5 > 2, swap them. List: [3, 4, 2, 5, 8].
4. Compare 5 and 8. No swap needed. List remains: [3, 4, 2, 5, 8].
• Third Pass:
1. Compare 3 and 4. No swap needed. List remains: [3, 4, 2, 5, 8].
2. Compare 4 and 2. Since 4 > 2, swap them. List: [3, 2, 4, 5, 8].
3. Compare 4 and 5. No swap needed. List remains: [3, 2, 4, 5, 8].
4. Compare 5 and 8. No swap needed. List remains: [3, 2, 4, 5, 8].
• Fourth Pass:
1. Compare 3 and 2. Since 3 > 2, swap them. List: [2, 3, 4, 5, 8].